import numpy as np
import pymysql


def getWeekData(userId):
	# 数据库连接参数
	# host = input("请输入服务器地址:")
	host = "210.42.42.130"
	port = 2003  # MySQL 默认端口
	user = 'root'
	password = "qwe123"
	# password = input("请输入密码:")
	db = 'summer_2'
	# 创建数据库连接
	connection = pymysql.connect(host=host, port=port, user=user, password=password, db=db)
	try:
		# 创建 cursor 对象
		with connection.cursor() as cursor:
			# 查询用户近一月的用电量
			sql = """
			select e_usage,e_time 
			from day_elec_info 
			where user_id=%s and e_time<CURRENT_DATE() and e_time>=DATE_SUB(CURDATE(), INTERVAL 12 DAY)
			order by e_time
			"""
			cursor.execute(sql, (userId,))
			result = cursor.fetchall()
			UsageList = []
			for row in result:
				e_usage, e_time = row
				UsageList.append(e_usage)
	except pymysql.MySQLError as e:
		print(f"Error: {e}")
		connection.rollback()  # 发生错误时回滚
	finally:
		# 关闭数据库连接
		connection.close()
	return UsageList

